import java.util.Date;

import models.EEntity;
import models.Organization;
import models.Request;
import models.Topic;
import models.TopicRelationNames;
import models.User;

import org.junit.Before;
import org.junit.Test;

import controllers.Application;
import controllers.RequestController;
import controllers.UserController;

import play.test.Fixtures;
import play.test.UnitTest;

public class heshamtest1 extends UnitTest {

	@Before
	public void delete() {
		Fixtures.deleteAll();
	}

	@Test
	public void test() {
		// Creating new organizations
		new Organization("Moss", "hh", 1).save();
		new Organization("Hesham", "ll", 1).save();
		new Organization("ali", "ll", 0).save();
		new Organization("Hesh", "ll", 2).save();
		// retrieving the,
		Organization o1 = Organization.find("byName", "Moss").first();
		Organization o2 = Organization.find("byName", "Hesham").first();
		Organization o3 = Organization.find("byName", "ali").first();
		Organization o4 = Organization.find("byName", "Hesh").first();
		// testing
		assertEquals(true, o1.isPrivate());
		assertEquals(true, o2.isPrivate());
		assertEquals(false, o3.isPrivate());
		assertEquals(false, o4.isPrivate());
	}
	@Test
	public void relateTopic(){
		User user = new User("Hesham", "male", "Hesham", "hey", new Date(),
				"H", "heyy").save();
		Organization o = new Organization("fe", "few", 0).save();
		EEntity entity = new EEntity("kolo", "heyy", o).save();

		Topic topic = new Topic("test topic", 0, "topic desc", new Date(),
				entity, user , false, false).save();
		
		Topic topic2 = new Topic("test topic2", 0, "topic desc2", new Date(),
				entity, user , false, false).save();
		
		TopicRelationNames relationName=new TopicRelationNames("relation").save();
		
		assertEquals(2, Topic.count());
		assertEquals(1, User.count());
		assertEquals(1,Organization.count());
		
		assertEquals(0,topic.relatedTopicList.size());
		assertEquals(0,topic2.relatedTopicList.size());
		

		user.relateToTopic(topic, topic2, relationName);
		user.save();
		
		assertEquals(1,topic.relatedTopicList.size());
		assertEquals(1,topic2.relatedTopicList.size());
		
		
		}
	@Test
	public void removeRelation(){
		
		User user = new User("Hesham", "male", "Hesham", "hey", new Date(),
				"H", "heyy").save();
		Organization o = new Organization("fe", "few", 0).save();
		EEntity entity = new EEntity("kolo", "heyy", o).save();

		Topic topic = new Topic("test topic", 0, "topic desc", new Date(),
				entity, user , false, false).save();
		
		Topic topic2 = new Topic("test topic2", 0, "topic desc2", new Date(),
				entity, user , false, false).save();
		
		TopicRelationNames relationName=new TopicRelationNames("relation").save();
		
		assertEquals(2, Topic.count());
		assertEquals(1, User.count());
		assertEquals(1,Organization.count());
		assertEquals(1,TopicRelationNames.count());
		assertEquals(0,topic.relatedTopicList.size());
		assertEquals(0,topic2.relatedTopicList.size());
		

		user.relateToTopic(topic, topic2, relationName);
		user.save();
		
		assertEquals(1,topic.relatedTopicList.size());
		assertEquals(1,topic2.relatedTopicList.size());
		
		
		user.removeRelation(topic, topic2);
		
		assertEquals(0,topic.relatedTopicList.size());
		assertEquals(0,topic2.relatedTopicList.size());
		assertEquals(0,topic.relationNames.size());
		assertEquals(0,topic2.relationNames.size());
		
	}
	
	
	
	
	
	

}
